package com.qingzhou.framework.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.qingzhou.common.enums.RateLimitType;
import com.qingzhou.common.utils.ContextUtil;
import com.qingzhou.common.utils.RedisUtil;
import com.qingzhou.common.utils.SecurityUtil;
import com.qingzhou.common.web.exception.ServiceException;
import com.qingzhou.framework.annotation.QzRateLimit;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.aspect.MethodInterceptor;
import org.noear.solon.core.wrap.MethodHolder;

import java.util.Collections;

/**
 * 限流处理
 * @author xm
 */
public class RateLimitAspect implements MethodInterceptor {

    @Inject
    RedisUtil redisUtil;

    @Override
    public Object doIntercept(Invocation inv) throws Throwable {
        return handleIntercept(inv);
    }

    /**
     * 限流实现
     * @param inv
     * @return
     */
    private Object handleIntercept(Invocation inv) throws Throwable {
        MethodHolder method = inv.method();
        QzRateLimit qzRateLimit = method.getAnnotation(QzRateLimit.class);
        // 拼接 key，形如：rate_limit:127.0.0.1-com.qingzhou.framework.filter.GlobalFilter-doFilter
        StringBuilder sb = new StringBuilder(qzRateLimit.key());
        if(qzRateLimit.type() == RateLimitType.TOKEN) {
            // 以 token限制
            String token = SecurityUtil.getTokenValue();
            if(StrUtil.isNotBlank(token)) {
                sb.append(token).append("-");
            }
        } else if (qzRateLimit.type() == RateLimitType.IP) {
            // 以 ip限制
            sb.append(ContextUtil.getIpAddr()).append("-");
        }
        sb.append(method.getDeclaringClz().getName()).append("-").append(method.getMethod().getName());
        int count = qzRateLimit.count();
        Long num = redisUtil.executeScript(limitScriptText(), Collections.singletonList(sb.toString()), count, qzRateLimit.time());
        if (ObjectUtil.isNull(num) || num.intValue() > count) {
            throw new ServiceException("访问过于频繁，请稍候再试");
        }
        return inv.invoke();
    }

    /**
     * 限流脚本
     */
    private String limitScriptText() {
        return "local key = KEYS[1]\n" +
                "local count = tonumber(ARGV[1])\n" +
                "local time = tonumber(ARGV[2])\n" +
                "local current = redis.call('get', key);\n" +
                "if current and tonumber(current) > count then\n" +
                "    return tonumber(current);\n" +
                "end\n" +
                "current = redis.call('incr', key)\n" +
                "if tonumber(current) == 1 then\n" +
                "    redis.call('expire', key, time)\n" +
                "end\n" +
                "return tonumber(current);";
    }

}
